﻿using System;

namespace c2
{
    class Program
    {

        public class Clock
        {
            private int time;
            public delegate void eventHandler();

            public event eventHandler Tick;
            private void IfTimeChanged()
            {
                if (Tick != null)
                {
                    Tick();
                }
                else
                {
                }
            }

            public event eventHandler Alarm;
            private void IfAlarm()
            {
                if (Alarm != null)
                {
                    Alarm();
                }
                else { }
            }
            
            public Clock(int second)
            {
                time = second;
                setTime(second);
                Alarmtime(second);
            }

            public void setTime(int second)
            {
                if (time != second)
                {
                    time = second;
                    IfTimeChanged();
                    Alarmtime(second);
                }
            }

            public void Alarmtime(int second)
            {
                if ((second%10)==0)
                {
                    IfAlarm();
                }
                else
                {
                }
            }

        }


        public class subscribEvent
        {
            public void printfTick()
            {
                Console.WriteLine("Tick");
            }
            public void printfAlarm()
            {
                Console.WriteLine("Alarm");
            }
        }



        static void Main(string[] args)
        {
            Console.WriteLine("时钟以秒为单位计算（取自系统秒数），每整十秒响铃一次");
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            int second = currentTime.Second;
            Clock clock = new Clock(second);
            subscribEvent v = new subscribEvent();
            clock.Tick += new Clock.eventHandler(v.printfTick);
            clock.Alarm += new Clock.eventHandler(v.printfAlarm);
            int i = 0;
            while (i<180)
            {
                currentTime = System.DateTime.Now;
                if ((second - currentTime.Second) != 0)
                {
                    second = currentTime.Second;
                    Console.WriteLine(second);
                    clock.setTime(second);
                    i = i + 1;
                }
            }
        }
    }
}
